package com.sanjiahantang.youjiarole.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.orhanobut.logger.Logger;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.login.views.impl.LoginActivity;
import com.sockect3d.DensityUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

//import com.tencent.bugly.Bugly;

/**
 * Created by Administrator on 2015/9/21.
 */
public class BaseApplication extends Application {

    {

        PlatformConfig.setWeixin(ConfigValue.WXAPP_ID, ConfigValue.WXAPP_SECRET);
    }

    private File extStorageAppBasePath;
    private File extStorageAppCachePath;

    @Override
    public void onCreate() {
        super.onCreate();

        UMShareAPI.get(this);

        initLogger();

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        FileDownloadLog.NEED_LOG = false;
        FileDownloader.setupOnApplicationOnCreate(this)
                .connectionCreator(new FileDownloadUrlConnection
                        .Creator(new FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15_000) // set connection timeout.
                        .readTimeout(15_000) // set read timeout.
                        .proxy(Proxy.NO_PROXY) // set proxy
                ))
                .commit();

        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(context, "eac3bd8e25", true, strategy);

        DensityUtil.init(getApplicationContext());
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    private void initLogger() {
        Logger.init("tag");
    }

    @Override
    public File getCacheDir() {

        if (extStorageAppCachePath != null) {
            return extStorageAppCachePath;
        } else {
            return super.getCacheDir();
        }
    }

    @Override

    protected void attachBaseContext(Context base) {

        super.attachBaseContext(base);

        MultiDex.install(base);

    }

    private static List<Activity> lists = new ArrayList<>();

    public static void addActivity(Activity activity) {
        lists.add(0, activity);
    }

    public static void clearActivity() {
        if (lists != null) {
            for (Activity activity : lists) {
                activity.finish();
            }

            lists.clear();
        }
    }

    private static List<Activity> baseList = new ArrayList<>();
    public static void addBaseList(Activity activity){
        if (!(activity instanceof LoginActivity)){
            baseList.add(0,activity);
        }
    }

    public static void clearBaseActivity(){
        if (baseList != null){
            for (Activity activity : baseList){
                if (activity != null && !(activity instanceof LoginActivity)){
                    activity.finish();
                }
            }
            baseList.clear();
        }
    }

}
